import React from "react";
import { connect } from "react-redux";
import { Popover } from "antd";
import menuRoute from "routes/menuRoute";
import { messages } from "share/common";
import airlinePlatformService from "./airline-platform.service";
import AdvanceTable from "components/advanced-table";

class airlinePlatform extends React.Component {
  constructor(props) {
    super(props);
    this.columns = [
      {
        title: messages("supplier-1.key441") /*公司ID*/,
        key: "companyCode",
        dataIndex: "companyCode",
        render: (code) => (
          <span>
            <Popover content={code}>{code || "-"}</Popover>
          </span>
        ),
      },
      {
        title: messages("supplier-1.key33") /*公司名称*/,
        key: "name",
        dataIndex: "name",
      },
    ];
    this.state = {
      configUrl: menuRoute.getRouteItem("airline-config", "key"),
      searchForm: [
        {
          component: "Input",
          id: "name",
          label: messages("supplier-1.key33") /*公司名称*/,
        },
        {
          component: "Input",
          id: "companyCode",
          label: messages("supplier-1.key441") /*公司ID*/,
        },
      ],
      data: [],
    };
  }

  createTableOption = () => {
    const { searchForm } = this.state;
    return {
      title: messages("supplier-1.key442") /*汇联易甄选机酒服务*/,
      key: "plane-hotel",
      service: this.advanceTableServer,
      onRef: this.advanceTableRef,
      rowKey: (record) => record.id,
      columns: this.columns,
      searchForm: searchForm,
      bordered: true,
      onRow: (record) => {
        return {
          onClick: () => {
            this.handleRowClick(record);
          },
        };
      },
    };
  };

  advanceTableRef = (ref) => {
    this.tableRef = ref;
  };

  advanceTableServer = (page, size, searchValues) => {
    this.searchParams = Object.assign({}, this.searchParams, searchValues, {
      page: page,
      size: size,
      role: "TENANT",
    });
    return airlinePlatformService.getCompanyList(this.searchParams);
  };

  handleRowClick = (record) => {
    const { configUrl } = this.state;
    this.context.router.push(
      configUrl.url.replace(":companyOID", record.companyOID).replace(":companyName", record.name)
    );
  };

  render() {
    const { tenant = {} } = this.props;
    const offSetHeight = tenant.resourceLayout === "T" ? "106px" : "64px";
    return (
      <div style={{ margin: "0 -20px" }}>
        <div style={{ height: `calc(100vh - ${offSetHeight})` }}>
          <AdvanceTable {...this.createTableOption()} />
        </div>
      </div>
    );
  }
}

airlinePlatform.contextTypes = {
  router: React.PropTypes.object,
};

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant,
  };
}

export default connect(mapStateToProps)(airlinePlatform);
